<!--#include file ="ASPHelpers.asp"-->

<%
var DB = {};

DB.RootDBPath = Server.MapPath("access/BackEnd");

DB.ConnectionStringBase = {
	Access:	"provider=microsoft.jet.oledb.4.0;data source=",
	SQL:	""
};

DB.DBFilePaths = {
	Scripts:			DB.RootDBPath + "\\Scripts.mdb",
	Admin:				DB.RootDBPath + "\\Admin.mdb",
	AfterHours:			DB.RootDBPath + "\\AfterHours_be.mdb",
	Help:				DB.RootDBPath + "\\help.mdb",
	HomeSmartCustomers:	DB.RootDBPath + "\\HomeSmart_Customers.mdb",
	Tickets:			DB.RootDBPath + "\\Tickets.mdb",
	Log:				DB.RootDBPath + "\\Log.mdb"		
};

DB.ConnectionStrings = {
	Scripts:			DB.ConnectionStringBase.Access + DB.DBFilePaths.Scripts,
	Admin:				DB.ConnectionStringBase.Access + DB.DBFilePaths.Admin,
	AfterHours:			DB.ConnectionStringBase.Access + DB.DBFilePaths.AfterHours,
	Help:				DB.ConnectionStringBase.Access + DB.DBFilePaths.Help,
	HomeSmartCustomers:	DB.ConnectionStringBase.Access + DB.DBFilePaths.HomeSmartCustomers,
	Tickets:			DB.ConnectionStringBase.Access + DB.DBFilePaths.Tickets,
	Log:				DB.ConnectionStringBase.Access + DB.DBFilePaths.Log
};

DB.CursorLocation = {
	None:		1,
	Server:		2,
	Client:		3
};

DB.CursorTypes = {
	ForwardOnly: 	0,
	KeySet:			1,
	Dynamic:		2,
	Static:			3
};

DB.LockTypes = {
	ReadOnly:			1,
	Pessimistic:		2,
	Optimistic:			3,
	BatchOptimistic:	4
};

DB.CommandTypes = {
	Text:		1,
	Table:		2,
	StoredProc:	4,
	Unkown:		8,
	File:		256,
	TableDirect:512	
}

DB.DataTypes = {
	Integer:	3,
	Boolean:	11,
	String:		129	
}

DB.DataCall = function(){
	this.connectionString = "";
	this.SQL = null;
	this.tableName = null;
	this.queryName = null;
	this.storedProcName = null;
	this.parameters = [];
	this.cursorLocation = 3;
	this.cursorType = DB.CursorTypes.ForwardOnly;
	this.lockType = DB.LockTypes.ReadOnly;
	this.commandType = DB.CommandTypes.Text;
	
	var dataCall = this;
	
	this.Execute = function(){
		var rs, conn, cat, cmd, curParam, commandParam, nothing;
		
		try{
			conn = Server.CreateObject("ADODB.Connection");
			conn.Open(dataCall.connectionString);
			
			rs = Server.CreateObject("ADODB.Recordset");
			rs.CursorLocation = dataCall.cursorLocation;
			rs.CursorType = dataCall.cursorType;
			rs.LockType = dataCall.lockType;
			nothing = rs.ActiveConnection; //this is a trick to get the otherwise ungettable value of Nothing to eventually set the ActiveConnection value back to
			
			cmd = Server.CreateObject("ADODB.Command");
			cmd.ActiveConnection = conn;
			cmd.CommandType = dataCall.commandType;
			cmd.CommandText = dataCall.SQL || dataCall.tableName || dataCall.queryName || dataCall.storedProcName;
			
			
			if(cmd.CommandType == DB.CommandTypes.StoredProc && dataCall.parameters.length){
				for(var param in dataCall.Params){
					curParam = dataCall.parameters[param];
								
					if(curParam.type == DB.DataTypes.String){
						commandParam = cmd.CreateParameter(param, curParam.type, 1, 255, curParam.value);
					}
					else{
						commandParam = cmd.CreateParameter(param, curParam.type, 1);
						commandParam.Value = curParam.value;
					}
					
					cmd.Parameters.Append(commandParam);
				}
			}
			
			rs.Open(cmd, conn);
		}
		catch(e){
			Response.Write("DB.DataCall Error: "e.message);
			Response.End();
		}
		finally{
			if(conn.State != 0){
				conn.Close();
			}
			conn = null;
			
			rs.ActiveConnection = nothing;
		}
		
		
		return rs;
		
	};
};




%>
